' tar -zxvf e:\WinVhds\work.tar -C e:\WinVhds\ work11B.vhdx
' cd e:
' cd \WinVhds
' tar -zcvf work.tar work11A.vhdx work11B.vhdx


dim fso
set fso = initFSO()

workDir = "\WinVhds"
usbDriver = findDrive(fso, workDir & "\work.cfg")
realDir = usbDriver & ":" & workDir

dim items(1)
items(0) = "A"
items(1) = "B"

' 逐个遍历，如使用过则重建
For Each itm In items
    vhdFile = realDir & "\work11" & itm & ".vhdx"
    If fileExists(fso, vhdFile) Then
        If vhdUsed(fso, vhdFile) Then
            If delFile(fso, vhdFile) Then
                createChildVhd fso, realDir, itm
            End If
        End If
    Else
        createChildVhd fso, realDir, itm 'size=4194304
    End If    
Next

' 逐个查找可启动项，找到则写入work.cfg文件
For Each itm In items
    vhdFile = realDir & "\work11" & itm & ".vhdx"
    If Not vhdUsed(fso, vhdFile) Then
        setStartVhd fso, realDir, itm
        Exit For
    End If
Next

set fso = Nothing

Function initFSO()
    set fso = CreateObject("Scripting.FileSystemObject")
    set initFSO = fso
End Function

Function findDrive(fso, fn)
    findDrive = ""

    Set drives = fso.Drives
    For Each d In drives
        fullfilename = d.DriveLetter & ":" & fn
        If fso.fileExists(fullfilename) Then
            findDrive = d.DriveLetter
            Exit For
        End If
    Next
    Set drives = Nothing
End Function

Function vhdUsed(fso, filespec)
    Dim f, res
    Set f = fso.GetFile(filespec)
    res = f.size > 4194304
    set f = Nothing
    vhdUsed = res
End Function

' Function vhdUsed(fso, filespec)
'     Dim f, res
'     Set f = fso.GetFile(filespec)
'     If f.size > 4194304 Then
'         res = True
'     Else
'         res = False
'     End If
'     set f = Nothing
'     vhdUsed = res
' End Function

Function delFile(fso, filename)
    res = False
    On Error Resume Next
    fso.DeleteFile filename, True
    If Err.Number = 0 Then
        res = True
    End If

    delFile = res
End Function

Function fileExists(fso, filespec)
    fileExists = fso.fileExists(filespec)
End Function

' Function copyFile(fso, src, dst, overwrite)
'     res = False
'     On Error Resume Next
'     fso.CopyFile src, dst, overwrite
'     If Err.Number = 0 Then
'         res = True
'     End If

'     copyFile = res
' End Function

Sub createFile(fso, src)
    Set f = fso.OpenTextFile(src, 2, True)
    f.Close
End Sub

Function getFileSize(fso, filespec)
    Dim f, res
    Set f = fso.GetFile(filespec)
    res = f.size
    set f = Nothing
    getFileSize = res
End Function

Sub runCmd(strCmd)
    set wssh = CreateObject("WScript.Shell")
    wssh.run strCmd, 0, True
    set wssh = Nothing
End Sub

' Sub createChildVhd(childvhd, parentvhd)
'     set wssh = CreateObject("WScript.Shell")
'     Set exec = wssh.Exec("diskpart.exe")
'     exec.StdIn.WriteLine "create vdisk file=" & childvhd & " parent=" & parentvhd
'     set exec = Nothing
'     set wssh = Nothing
' End Sub

' Sub createChildVhd(fso, childvhd, parentvhd, realDir)
'     scrfile = realDir & "\vhd.script"
'     writeFile fso, scrfile, "create vdisk file=" & childvhd & " parent=" & parentvhd
'     runCmd "diskpart.exe /s " & scrfile
'     delFile fso, scrfile
' End Sub

Sub createChildVhd(fso, path, cname)  
    tarName = path & "\work.tar" 'path=e:\WinVhds
    vhdname = "work11" & cname & ".vhdx"
    runCmd "tar -zxvf " & tarName & " -C " & path & "\ " & vhdname   'tar -zxvf e:\WinVhds\work.tar -C e:\WinVhds\ work11A.vhdx
End Sub

Sub setStartVhd(fso, path, vhdname)
    set fl = fso.OpenTextFile(path & "\work.cfg", 2, True) 'If not exist, create file.
    fl.WriteLine "vhdboot=work11" & vhdName & ".vhdx"
    fl.Close
End Sub

Sub writeFile(fso, filespec, text)
    Set fl = fso.OpenTextFile(filespec, 2, True) 'If not exist, create file.
    fl.WriteLine(text)
    fl.Close
End Sub

chmod +x /etc/rc.d/rc.local
chmod +x /etc/rc.d/recovery.sh
/etc/rc.d/recovery.sh

